﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.WindowsMobile;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
using System.Threading;
using System.IO;
using System.Reflection;



namespace SMSController
{
    public class Init
    {
        public Init()
        {
            //create a event, that will be active by a new sms arrive;
            MessageInterceptor interceptor = new MessageInterceptor();
            interceptor.MessageReceived += new MessageInterceptorEventHandler(interceptor_MessageReceived);
            //Rules rulers = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");

            //MessageBox.Show(rulers.getAllShortcutTitle(";&&;&&;"));
            //MessageBox.Show(Convert.ToString(rulers.delAcceptableNumber("13910256732")));
            //Rules rule = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");
            //MessageBox.Show(rule.getHead());
            //MessageBox.Show(Convert.ToString(rule.setHead(@"do")));
            Application.Run(new frmSetting());
            
            
        }
        
        //Do MessageReceived Events
        private void interceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
            SmsMessage sms = e.Message as SmsMessage;
            Rules rules = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");
            string cmdHead = rules.getHead();
            //analysis sms body, sms body must include initial word "doexec"
            string smsBody = trimDoubleSpace(sms.Body.Trim());
            if (
                rules.isAll() 
                || 
                rules.isAcceptableNumber(sms.From.Address.ToString().Trim())
                )
            {
                //do command
                string headMarker, toggleSwitch, shortcutTitle;
                try
                {
                    string[] tmp=smsBody.Split(' ');
                    headMarker=tmp[0];
                    toggleSwitch = tmp[1];
                    shortcutTitle = tmp[2];
                }
                catch
                {
                    return;
                }
                if (headMarker.ToLower().CompareTo(cmdHead.Trim().ToLower()) == 0)
                {
                    switch (toggleSwitch)
                    {
                        case "shortcuts":
                            using (Process proc = new Process())
                            {
                                proc.StartInfo.FileName = rules.getShortcutCommand(shortcutTitle);
                                proc.StartInfo.Arguments = rules.getShortcutParameter(shortcutTitle);
                                proc.Start();
                                if (proc.HasExited)
                                    proc.Kill();
                            }
                            return;
                        case "command":
                            return;
                        default:
                            return;

                    }
                }
            }

        }
        private string trimDoubleSpace(string inputString)
        {
            try
            {
                return trimDoubleSpace(inputString.Replace("  ", " "));
            }
            catch
            {
                return inputString;
            }
        }
    }
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [MTAThread]
        static void Main()
        {
            //only one process can be run.
            Mutex mu = new Mutex(false);
            bool ProcessExist = !mu.WaitOne(0, false);

            if (!ProcessExist)
            {
                new Init();
            }
            else
            {
                Application.Exit();
            }
        }
    }
}